var express = require('express');
var router = express.Router();
var Users = require('../../db/UsersIf');
var querystring = require('querystring');
var util = require('util');


router.get('/', function(req, res, next) {
  console.log('req.session.id:' + req.session.id);
  if(req.session.user) {
    var userId = req.session.user.id;
    //todo get cloudapps from mongodb
    var json = {errCode: 0, errMsg: ''};
    res.json(json);

  } else {
    var json = {errCode: 10001, errMsg: 'no login'};
    res.json(json);
  }
});

router.post('/*', function(req, res, next) {
  console.log('req.session.id:' + req.session.id);
  if(req.session.user) {
  	console.log('req.session.user:query ' + req.query);
  	var postdata = '';
    req.on('data', function(chunk) {
    	console.log('data');
      postdata += chunk;
      console.log(postdata);
    });

    req.on('end', function() {
      postdata = querystring.parse(postdata);
      console.log("recv postdata:" + util.inspect(postdata));
    	var json = {errCode: 0, errMsg: ''};
		res.json(json);
    });

  } else {
  	var json = {errCode: 10001, errMsg: 'no login'};
  	res.json(json);
  }
});


router.delete('/*', function(req, res, next) {
  console.log('req.session.id:' + req.session.id);
  if(req.session.user) { 
    var projectId = '';
    if(req.path.length > 0) {
      projectId = req.path.substring(1);
    }
    
    console.log('projectId:' + projectId);
    var json = {errCode: 0, errMsg: ''};
    res.json(json);
  } else {
    var json = {errCode: 10001, errMsg: 'no login'};
    res.json(json);
  }
  
});

router.patch('/*', function(req, res, next) {
  console.log('req.session.id:' + req.session.id);
  if(req.session.user) {
    var projectId = '';
    if(req.path.length > 0) {
      projectId = req.path.substring(1);
    }
    var projectName = req.query.project_name;
    console.log('projectId:' + projectId + ',projectName:' + projectName);
    var json = {errCode: 0, errMsg: ''};
    res.json(json);
  } else {
    var json = {errCode: 10001, errMsg: 'no login'};
    res.json(json);
  }
  
});

module.exports = router;